package com.imooc.aspect;


import com.imooc.checkparams.CheckParams;
import com.imooc.domain.Boy;
import com.imooc.result.Result;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.*;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


/**
 * Created by chenxin on 2017/4/30.
 */
@Aspect
@Component
public class HttpAspect {

    public final static Logger loger = LoggerFactory.getLogger(HttpAspect.class);

    @Pointcut("execution(public * com.imooc.controller.*.*(..))")
    public void log() {

    }

    @Before("log()")
    public void brforedo(JoinPoint joinPoint) throws Exception {
        loger.info("启动前面{}", 1111111);
        ServletRequestAttributes attributeEvent = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attributeEvent.getRequest();
        //获取url
        loger.info("路径地址{}", request.getRequestURL());
        String url = request.getRequestURL().toString();
        System.out.println("获取的url路近视：" + url);
        String urls = request.getRequestURI().substring(request.getContextPath().length());
        System.out.println("这是截取的url路劲:" + urls);
        switch (urls) {
            case "/boy/savaone":
                System.out.println("进来了");
                //校验参数
                Result result = CheckParams.checksaveone(request);
                if (result != null) {
                    throw new Exception("DDD");
                }
            default:
                System.out.println("meizdddd+++++++==");

        }

        //获取方法
        loger.info("方法{}", request.getMethod());
        //获取ip
        loger.info("ip{}", request.getRemoteAddr());

        //获取类方法
        loger.info("获取的类方法{}", joinPoint.getSignature().getDeclaringTypeName() + "." + joinPoint.getSignature().getName());

        //获取参数
        loger.info("参数={}", joinPoint.getArgs());
    }

    @After("log()")
    public void doArfter() {
        loger.info("启动后面{}", 22222222);
    }

    @AfterReturning(returning = "object", pointcut = "log()")
    public void doAfodoing(Object object) {
       // loger.info("response={}", object.toString());

    }

}
